package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.*;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.utils.TokenUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.example.springboot.service.IGuideService;
import com.example.springboot.entity.Guide;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/guide")
    public class GuideController {
    
    @Resource
    private IGuideService guideService;

    //更新引导信息
    @PostMapping
    public Result update(@RequestBody Guide guide) {
        guide.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return Result.success("创新引导保存成功",guideService.saveOrUpdate(guide));
    }

    //新增引导信息
    @PostMapping("/insert")
    public Result insert(@RequestBody Guide guide) {
        guide.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return guideService.insertGuide(guide);
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = guideService.removeGuideInfo(id);
        if (result)
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"引导不存在");
    }

    //根据id批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return guideService.removeByIds(ids);
    }

    //查找所有
    @GetMapping
    public List<Guide> findAll() {
        return guideService.list();
    }

    //根据id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(guideService.getById(id));
    }

    //根据当前用户id和搜索的引导内容批量查找
    @GetMapping("/search")
    public List<Guide> findByName(@RequestParam String name) {
        return guideService.findByName(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId(),name);
    }

    //获取关系信息
    @GetMapping("/getModelLinks")
    public Result getModelLinks(@RequestParam Integer id) throws JsonProcessingException {
        return guideService.getModelLinks(id);
    }

    //根据用户id查找
    @GetMapping("/currentUser")
    public Result findByUser() {
        return guideService.findByUser(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
    }

    //根据引导id判断是否为当前用户的引导
    @GetMapping("/isMine")
    public Result isMine(@RequestParam Integer id) {
        Integer userId = Objects.requireNonNull(TokenUtils.getCurrentUser()).getId();
        QueryWrapper<Guide> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("user_id",userId);
        Guide guide;
        try {
            guide = guideService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (guide == null)
            return Result.error(Constants.CODE_400,"参数错误");
        return Result.success(true);

    }

    //前端传入JSON字符串，生成创新方向列表
    @PostMapping("/createDirection")
    public Result createDirection(@RequestBody Map<String,String> jsonMap) throws JsonProcessingException {
        return guideService.createDirection(jsonMap.get("jsonString"));
    }

    //关键词推荐
    @PostMapping("/recommendKeyWord")
    public Result recommendKeyWord(@RequestBody List<Integer> num){
        return guideService.recommendKeyWord(num);
    }
}

